package com.hxb.burp.utils;

import org.ini4j.Ini;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * burp_extendsion2
 *
 * @author 黄小斌
 * @date 2023-10-27
 */
public class IniUtils {


    private static final File CONFIG_FILE = new File(System.getProperty("user.home") + "/.burp_custom.ini");


    static {
        init();
    }

    private static void init() {
        try {
            if (!CONFIG_FILE.exists()) {
                boolean createFile = CONFIG_FILE.createNewFile();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getProperties(String section, String key, String defaultValue) {
        String result = null;
        try {
            Ini ini = new Ini();
            ini.load(CONFIG_FILE);
            result = ini.get(section, key);
        } catch (IOException ignored) {
        }
        return result == null ? defaultValue : result;
    }

    public static List<IniConfig> getPropertiesList(String section) {
        List<IniConfig> result = new ArrayList<>();
        try {
            Ini ini = new Ini();
            ini.load(CONFIG_FILE);
            ini.getAll(section).forEach(item -> {
                item.forEach((itemKey, itemValue) -> {
                    result.add(new IniConfig(itemKey, itemValue));
                });
            });

        } catch (IOException ignored) {
        }
        return result;
    }

    public static void addProperties(String section, String key, String value) {
        try {
            Ini ini = new Ini();
            ini.load(CONFIG_FILE);
            ini.put(section, key, value);
            ini.store(CONFIG_FILE);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static void addPropertiesList(String section, List<IniConfig> list) {
        try {
            Ini ini = new Ini();
            ini.load(CONFIG_FILE);
            for (IniConfig entity : list) {
                if (entity.getKey() != null && !entity.getKey().trim().isEmpty()) {
                    ini.put(section, entity.getKey(), entity.getValue());
                }
            }
            ini.store(CONFIG_FILE);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static class IniConfig {
        public IniConfig(String key, Object value) {
            this.key = key;
            this.value = value;
        }

        private String key;

        private Object value;


        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {
        getPropertiesList("test").forEach(item -> {
            System.out.println(item.key + "==>" + item.value);
        });
    }
}
